import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _Button2 from "choerodon-ui/lib/button";
import _Button from "choerodon-ui/pro/lib/button";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
var _excluded = ["type", "c7nType"],
    _excluded2 = ["permissionList", "className"];

var _dec, _dec2, _dec3, _dec4, _class, _class2;

import React from 'react';
import PropTypes from 'prop-types';
import { Button as CpaasButton } from 'antd';
import { Bind } from 'lodash-decorators';
import { PENDING, SUCCESS } from "../Status";
var Button = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), (_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(Button, _React$Component);

  var _super = _createSuper(Button);

  function Button() {
    var _this;

    _classCallCheck(this, Button);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      status: PENDING,
      controllerType: 'disabled'
    };
    return _this;
  }

  _createClass(Button, [{
    key: "componentDidMount",
    value: // 在 render 之前检查权限， permissionList
    // eslint-disable-next-line
    function componentDidMount() {
      var _this$props$permissio = this.props.permissionList,
          permissionList = _this$props$permissio === void 0 ? [] : _this$props$permissio;

      if (permissionList !== undefined && Array.isArray(permissionList)) {
        this.check(this.props, this.context);
      }
    }
    /**
     * 调用 context 的 check
     * @param {object} props - 检查所需参数
     * @param {object} context - 上下文
     */

  }, {
    key: "check",
    value: function check(props, context) {
      var _props$permissionList = props.permissionList,
          permissionList = _props$permissionList === void 0 ? [] : _props$permissionList;

      if (context.permission) {
        context.permission.check({
          permissionList: permissionList
        }, this.handlePermission);
      }
    }
  }, {
    key: "handlePermission",
    value: function handlePermission(status) {
      var controllerType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'disabled';
      this.setState({
        status: status,
        controllerType: controllerType
      });
    }
  }, {
    key: "renderButton",
    value: function renderButton() {
      var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

      var _params$type = params.type,
          type = _params$type === void 0 ? '' : _params$type,
          _params$c7nType = params.c7nType,
          c7nType = _params$c7nType === void 0 ? 'button' : _params$c7nType,
          otherProps = _objectWithoutProperties(params, _excluded);

      switch (type) {
        case 'text':
          return /*#__PURE__*/React.createElement("a", otherProps, params.children);

        case 'c7n-pro':
          return /*#__PURE__*/React.createElement(_Button, Object.assign({}, otherProps, {
            type: c7nType
          }));

        case 'c7n':
          return /*#__PURE__*/React.createElement(_Button2, Object.assign({}, otherProps, {
            type: c7nType
          }));

        default:
          return /*#__PURE__*/React.createElement(CpaasButton, Object.assign({}, otherProps, {
            type: type
          }));
      }
    }
  }, {
    key: "extendProps",
    value: function extendProps() {
      var _this$props = this.props,
          permissionList = _this$props.permissionList,
          propsClass = _this$props.className,
          otherProps = _objectWithoutProperties(_this$props, _excluded2);

      var _otherProps$style = otherProps.style,
          style = _otherProps$style === void 0 ? {} : _otherProps$style;
      var _this$state = this.state,
          status = _this$state.status,
          controllerType = _this$state.controllerType;
      var className = [propsClass, 'cpaas-permission-btn'].join(' '); // 普通按钮不做限制

      if (permissionList === undefined) {
        return this.renderButton(otherProps);
      } // 鉴权通过后的处理


      if (status === SUCCESS) {
        if (controllerType === 'disabled') {
          return this.renderButton(_objectSpread(_objectSpread({}, otherProps), {}, {
            className: [className, 'cpaas-permission-btn-disabled'].join(' '),
            disabled: true,
            style: _objectSpread(_objectSpread({}, style), {}, {
              cursor: 'not-allowed',
              color: 'rgba(0,0,0,0.25)'
            })
          }));
        } else {
          // approved=true，则controllerType=disabled则禁用，其他，则不控制
          return this.renderButton(_objectSpread(_objectSpread({}, otherProps), {}, {
            className: className
          }));
        }
      } else {
        // 鉴权失败，返回null，不显示内容
        return null;
      }
    }
  }, {
    key: "render",
    value: function render() {
      return this.extendProps();
    }
  }]);

  return Button;
}(React.Component), _class2.contextTypes = {
  permission: PropTypes.object
}, _class2), (_applyDecoratedDescriptor(_class.prototype, "check", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "check"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handlePermission", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handlePermission"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "renderButton", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "renderButton"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "extendProps", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "extendProps"), _class.prototype)), _class));
export { Button as default };